package org.itboys.shop.controller;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.admin.tools.AdminCookieHolder;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.AceUtils;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.seller.constant.SellerConstants;
import org.itboys.seller.entity.SellerUser;
import org.itboys.seller.service.SellerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 商家账号 
 * @author weisky
 *
 */
@RestController
@RequestMapping("/seller/user")
public class SellerUserController extends BaseController{

    @Autowired
    private SellerUserService sellerUserService;

    /**
     * 总平台：跳转到商家账号页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/toListVM")
    public ModelAndView toListVM(Long sellerId, Model model,
                                 HttpServletRequest request, HttpServletResponse response) {
        if (sellerId == null) {
            String userName = AdminCookieHolder.getAdminUserName();
            sellerId = sellerUserService.getShopId(userName);
        }
        model.addAttribute("sellerId", sellerId);
        return new ModelAndView("/seller/user-list");
    }

    @RequestMapping("/list")
    public void list(@RequestParam long sellerId, @RequestParam int type,
                     HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
        param.put("sellerId", sellerId);
        String userName = AdminCookieHolder.getAdminUserName();
        if (type == 1) {  //运营平台
            param.put("type", SellerConstants.TYPE_SUPER);
        } else {  //店铺平台
            SellerUser proUser = sellerUserService.getAdminUser(userName);
            if (proUser.getType() == SellerConstants.TYPE_COMMON) {
                JsonPageUtils.renderJsonPage(0L, Lists.newArrayList(), response);
                return;
            }
            param.put("type", SellerConstants.TYPE_COMMON);
        }

        String name = (String) param.get("userName");
        if(StringUtils.isNotBlank(name)){//根据商家名称  模糊查询
            param.put("userName", AceUtils.getPattern(name));
        }
        Page<SellerUser> page = sellerUserService.pageQuery(param, request);
        JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
    }

    /**
     * 保存或者修改
     * @param sellerUser
     * @param logos
     * @param request
     * @param response
     */
    @RequestMapping("/save")
    public void save(SellerUser sellerUser,
                     @RequestParam(required=false) MultipartFile logos,
                     HttpServletRequest request, HttpServletResponse response){
        int result = sellerUserService.insert(sellerUser, logos);
        if(result == SellerConstants.VALUE_EXIST){//账号已存在
            AjaxUtils.renderText(response, "exist");
        }else if(result == SellerConstants.ACTION_SUCCESS){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    @RequestMapping("/getById")
    public void getById(Long sellerUserId, HttpServletResponse response){
        AjaxUtils.renderJson(response, sellerUserService.getById(sellerUserId));
    }

    /**
     * 修改商家账号状态
     * @param sellerUserId
     * @param state
     * @param response
     */
    @RequestMapping("/updateSellerUserState")
    public void updateSellerUserState(Long sellerUserId, int state, HttpServletResponse response){
        boolean result = sellerUserService.updateState(sellerUserId, state);
        if(result){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 删除
     * @param sellerUserId
     * @param response
     */
    @RequestMapping("/delete")
    public void delete(Long sellerUserId, HttpServletResponse response){
        boolean result = sellerUserService.dele(sellerUserId);
        if(result){
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        }else{
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

}
